package groovy

import okhttp3.*
//def httpRequestInstance = new GroovyClassLoader().parseClass(new File("/home/zzj/groovy/groovytest1/src/main/java/org/example/request/HttpRequest.groovy")).newInstance()
class HttpRequest {
    private static String defaultUrl="http://192.168.1.18:8080"
    static Response getRequest(String url=defaultUrl,String path="",Map<String,String> params=null) {
        // 构建基础 URL
        HttpUrl baseUrl = HttpUrl.parse(url+path);
        // 在基础 URL 上添加查询参数
//            HttpUrl urlWithParams
        def builder= baseUrl.newBuilder()
        if (params!=null)
        {
            params.each {key,value->{builder.addQueryParameter(key, value)}}
        }
        def urlWithParams=builder.build()
        // 构建请求对象
        Request request = new Request.Builder()
                .url(urlWithParams)
                .get()
                .build();
        new OkHttpClient()
                .newCall(request)
                .execute()
    }
    static Response postRequest(String url=defaultUrl,String path="",Map<String,String> params=null,String body=""){
        HttpUrl baseUrl = HttpUrl.parse(url+path);
        // 在基础 URL 上添加查询参数
//            HttpUrl urlWithParams
        def builder= baseUrl.newBuilder()
        if (params!=null)
        {
            params.each {key,value->{builder.addQueryParameter(key, value)}}
        }
        def urlWithParams=builder.build()
        // 构建请求对象
        RequestBody requestBody = RequestBody.create(
                body, MediaType.get("application/json; charset=utf-8"));
        Request request = new Request.Builder()
                .url(urlWithParams)
                .post(requestBody)
                .build();
        new OkHttpClient()
                .newCall(request)
                .execute()
    }
    static def getId2ById1(String id1) {
        Response response=getRequest(defaultUrl,"/getId2ById1",["id1":id1])
        def responseBody = response.body()
        if (response.isSuccessful()) {
            def responseString = responseBody.string()
            println "Success: $responseString"
            return responseString
        } else {
            if (response.code()==404){
                return -1;
            }
            else
                throw new Exception( "Request failed: ${response.code()} ${responseBody.string()}")
        }
    }
    static String getParentById1(String id1) {
        Response response=getRequest(defaultUrl,"/getParentById1",["id1":id1])
        def responseBody = response.body()
        if (response.isSuccessful()) {
            def responseString = responseBody.string()
            println "Success: $responseString"
            return responseString
        } else {
            if (response.code()==404){
                return "-1";
            }
            else
                throw new Exception( "Request failed: ${response.code()} ${responseBody.string()}")
        }
    }
    static def saveIdRecord(String idRecords) {
        Response response=postRequest(defaultUrl,"/saveIdRecord",null,idRecords)
//        RequestBody body = RequestBody.create(
//                idRecords, MediaType.get("application/json; charset=utf-8"));
//        def request = new Request.Builder()
//                .url("${defaultUrl}/saveIdRecord")
//                .post(body)
//                .build()
//        Response response = new OkHttpClient()
//                .newCall(request)
//                .execute()
        def responseBody = response.body()
        if (response.isSuccessful()) {
            def responseString = responseBody.string()
            println "Success: $responseString"
            return responseString
        }
        else
            throw new Exception( "Request failed: ${response.code()} ${responseBody.string()}")
    }
    static String cancelTask(String activityId,String processInstanceId) {
        println(getRequest("http://192.168.1.14:8000", "/cancelTask", ["activity_id": activityId, "process_instance_id": processInstanceId]))
    }
    }



